package com.corn3lius.android.hexagonmadness;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Point;
import android.graphics.Rect;

public class Hex {

	public int color;
	public int direction;
	public int type;
	public Point position;
	// user has finger over this piece
	public boolean inHover;
	public int width = 58;
	public int height = 52; 
	public int animation = 0;
	
	// Default
	Hex() {
		color = HexView.COL_RED;
		direction = HexView.DIR_NONE;
		type = HexView.NORMAL;
		position = new Point(0, 0);
		inHover = false;
	}

	/**
	 * 
	 * @param col
	 * @param pos
	 */
	Hex(int col, Point pos) {
		color = col;
		position = new Point(pos);
		inHover = false;
		direction = HexView.DIR_NONE;
		type = HexView.NORMAL;
	}

	/**
	 * Constructor 
	 * @param col this is the color index
	 * @param dir direction index
	 * @param pos this is the position. 
	 */
	Hex(int col, int dir, Point pos) {
		color = col;
		position = new Point(pos);
		inHover = false;
		direction = dir;
		type = HexView.NORMAL;
	}

	public Hex(Hex h) {
		color = h.color;
		position = new Point(h.position);
		inHover = false;
		direction = h.direction;
		type = h.type;
	}

	/*
	 * Allow the board to call this to move the hex
	 */
	public void move(Point newP) {
		position.set(newP.x, newP.y);
	}

	/*
	 * This function is used to determine the next position for the piece it
	 * takes current position and increments it toward the pieces direction.
	 */
	public Point getNextPosition() {
		Point newP = new Point(position);
		int yofx = ((position.x + 1) % 2);
		switch (direction) {
		case HexView.DIR_UP:
			newP.offset(0, -1);
			break;
		case HexView.DIR_DN:
			newP.offset(0, 1);
			break;
		case HexView.DIR_UP_R:
			newP.offset(1, yofx - 1);
			break;
		case HexView.DIR_UP_L:
			newP.offset(-1, yofx - 1);
			break;
		case HexView.DIR_DN_R:
			newP.offset(1, yofx);
			break;
		case HexView.DIR_DN_L:
			newP.offset(-1, yofx);
			break;
		}
		return newP;
	}

	/** 
	 * 
	 *  
	 */
	public List<Point> getPositionArray() {
		List<Point> pList = new ArrayList<Point>();
		int yofx = ((position.x + 1) % 2);
		Point newP = new Point(position);
		for (int i = 0; i < 8; i++) {
			yofx = ((newP.x + 1) % 2);
			switch (direction) {
			case HexView.DIR_UP:
				newP.offset(0, -1);
				break;
			case HexView.DIR_DN:
				newP.offset(0, 1);
				break;
			case HexView.DIR_UP_R:
				newP.offset(1, yofx - 1);
				break;
			case HexView.DIR_UP_L:
				newP.offset(-1, yofx - 1);
				break;
			case HexView.DIR_DN_R:
				newP.offset(1, yofx);
				break;
			case HexView.DIR_DN_L:
				newP.offset(-1, yofx);
				break;
			}
			pList.add(new Point(newP));
		}
		return pList;
	}

	/**
	 * Use this function to set the piece on the board
	 * 
	 * @return
	 */
	public boolean setInPlay(Point pos, int dir) {
		direction = dir;
		position.set(pos.x, pos.y);
		return true;
	}

	/**
	 * This is a compare function to check for sets.
	 */
	public boolean inSetWith(Hex h) {
		if (this.equals(h))
			return false;
		if (h.color == this.color) {
			int yofx = ((position.x + 1) % 2);
			if (h.position.x == position.x) {
				// check above or below
				return ((h.position.y == (position.y - 1)) || 
						(h.position.y == (position.y + 1)));
			} else if (h.position.x == (position.x + 1)) {
				// check to the right
				return ((h.position.y == (position.y + (yofx - 1))) || 
						(h.position.y == (position.y + yofx)));        
			} else if (h.position.x == (position.x - 1)) {
				// check to the left
				return ((h.position.y == (position.y + (yofx - 1))) || 
						(h.position.y == (position.y + yofx)));
			}
		}
		return false;
	}
	
	
	/**
	 * ************************************************ 
	 * 
	 * These two functions should be refactored to the 
	 * thread class since it will handle where the hex 
	 * is actually drawn based on the size of the view.  
	 * 
	 * ************************************************
	 */
	/** 
	 * This gets the offset in the image to the color and my specific animation set. 
	 * @return
	 */
	public Rect getSrc() {
		int y = height * color; 
		int x = width  * animation;
		return new Rect(x,y, x + width, y + height );
	}
	
	public Rect getDirSrc() {
		int y = height * 9;
		int x = width * direction; 
		return new Rect(x,y, x + width, y + height );		
	}
}
